package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Reference
    HouseService houseService;

    @Reference
    CommunityService communityService;

    @Reference
    HouseImageService houseImageService;

    @Reference
    HouseBrokerService houseBrokerService;


    @Reference
    UserFollowService userFollowService;
    @GetMapping("/info/{id}")
    public Result<Map<String, Object>> list(@PathVariable("id") Long id, HttpServletRequest request){

        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        //----------补充代码 22.6.25 将isFollow变成动态--------------
        boolean isFollow = false;
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        if (userInfo != null) {
            Long userInfoId = userInfo.getId();
            isFollow = userFollowService.isFollow(userInfoId, id);
        }


        Map map = new HashMap<>();
        map.put("house", house);
        map.put("community", community);
        map.put("houseBrokerList", houseBrokerList);
        map.put("houseImage1List", houseImage1List);
        map.put("isFollow", isFollow);

        return Result.ok(map);
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<HouseVo>> findListPage(@PathVariable("pageNum") Integer pageNum,
                                                  @PathVariable("pageSize") Integer pageSize,
                                                  @RequestBody HouseQueryVo houseQueryVo) {

        PageInfo<HouseVo> page = houseService.findListPage(pageNum, pageSize, houseQueryVo);


        return Result.ok(page);//将分页数据封装在Result中返回

    }
}
